package mq

import (
	"gitee.com/mkwchecking/sr-core/yal/ymap"
	"github.com/google/wire"
)

// mq 集
var (
	_mqSet   = ymap.NewSetMap[IMsgQueue]()
	Provider = wire.NewSet(Install)
)

func Install(cfg *Config) (ymap.ISet[IMsgQueue], error) {
	mqSet, err := New(cfg)
	if err != nil {
		return nil, err
	}

	_mqSet = mqSet
	return _mqSet, nil
}

func New(cfg *Config) (ymap.ISet[IMsgQueue], error) {
	defaultMq, err := NewConn(cfg.Conn)
	if err != nil {
		return nil, err
	}

	newSet := ymap.NewSetMap[IMsgQueue]()
	newSet.SetDefault(defaultMq)

	for _, resolve := range cfg.Resolves {
		mq, err := NewConn(resolve)
		if err != nil {
			return nil, err
		}

		newSet.Set(resolve.Engine.Str(), mq)
	}
	return newSet, nil
}
